//配置信息类
function SK_Config(){

}

SK_Config.Debug = true;

/**
 * 工具类
 */
function SK_Tool(){

}

SK_Tool.log = function(obj){
    if (SK_Config.Debug) {
        console.log(obj);
    }
}
//隐藏对象
SK_Tool.hideObject = function(obj){
    obj.style.display = "none";
}
//显示对象
SK_Tool.showObject = function(obj){
    obj.style.display = "block";
}
//是否已经显示
SK_Tool.isShow = function(obj){
    return (obj.style.display == "block");
}



/**
 * List集合
 */
function SK_List(){
    this.data = new Array();
    
    this.add = function(d){
        this.data.push(d);
    }
    
    this.get = function(index){
        return this.data[index];
    }
    
    this.remove = function(obj){
        var i = this.indexOf(k);
        if (i != -1) {
            this.keys.splice(i, 1);
        }
    }
    
    this.clear = function(){
    	this.data = new Array();
    }
    
    this.size = function(){
        return this.data.length;
    }
    
    this.contains = function(k){
        return this.data[k] != null;
    }
    
    this.isEmpty = function(){
        return this.data.length === 0;
    }
    
    this.indexOf = function(obj){
        var size = this.size();
        if (size > 0) {
            for (var i = 0; i < size; i++) {
                if (this.data[k] == obj) {
                    return i;
                }
            }
        }
        return -1;
    }
    
    this.toString = function(){
  		var str = "[";
        var size = this.size();
        for (var i = 0; i < size; i++, str += ",") {
            str += this.data[i];
        }
        str = str.substring(0, str.length - 1);
        str += "]";
        return str;
    }
}

/**
 * Map集合
 */
function SK_Map(){
    this.keys = new Array();
    this.values = new Object();
    
    this.put = function(k, v){
        if (this.values[k] == null) {
            this.values[k] = v;
        }
        this.keys.push(k);
    }
    
    this.isEmpty = function(){
        return this.keys.length === 0;
    }
    
    this.contains = function(k){
        return this.values[k] != null;
    }
    
    this.get = function(k){
        return this.values[k];
    }
    
    this.remove = function(k){
        var i = this.indexOf(k);
        if (i != -1) {
            this.keys.splice(i, 1);
        }
        this.values[k] = null;
    }
    
    this.indexOf = function(k){
        var size = this.size();
        if (size > 0) {
            for (var i = 0; i < size; i++) {
                if (this.keys[k] == k) {
                    return i;
                }
            }
        }
        return -1;
    }
    
    this.size = function(){
        return this.keys.length;
    }
    
    this.clear = function(){
        this.keys = new Array();
        this.values = new Object();
    }
    
    this.toString = function(){
        var str = "{";
        var size = this.size();
        for (var i = 0; i < size; i++, str += ",") {
            var key = this.keys[i];
            var value = this.values[key];
            str += key + ":" + value;
        }
        str = str.substring(0, str.length - 1);
        str += "}";
        return str;
    }
}

//随机类
function SK_Random(){

}

SK_Random.D26 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
SK_Random.D36 = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
SK_Random.X26 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
SK_Random.X36 = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
SK_Random.E62 = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

SK_Random.randomInt = function(min, max){
    return parseInt(Math.random() * (max - min + 1) + min);
}

SK_Random.randomPercent = function(v){
    var rd = Math.random() * 100;
    return (rd <= v);
}

SK_Random.randomString = function(type, length){
    var size = type.length - 1;
    var str = "";
    for (var i = 0; i < length; i++) {
        var index = SK_Random.randomInt(0, size);
        str += type[index];
    }
    return str;
}

SK_Random.randomStringLength = function(type, minLength, maxLength){
    var length = SK_Random.randomInt(minLength, maxLength);
    return SK_Random.randomString(type, length);
}

/**
 * 任务对象
 * @param {Object} callback
 * @param {Object} date
 * @param {Object} groupName
 * @param {Object} jobName
 */
function SK_Task(callback, date, groupName, jobName,obj){
	var task = this;
    task.callback = callback;
    task.date = date;
    task.groupName = groupName;
    task.jobName = jobName;
    task.obj = obj;
    task.run = function(){
        SK_Tool.log('SK_Task.run  GroupName:' + groupName + ' JobName:' + jobName)
        var group = SK_TaskTime.tasks.get(groupName);
        if (group) {
            var timeOut = group.get(jobName);
            if (timeOut) {
                group.remove(jobName);
                if (group.isEmpty()) {
                    group.remove(groupName);
                }
            }
        }
        callback(task);
    }
}

/**
 * 定时任务管理器
 */
function SK_TaskTime(){

}

//任务组
SK_TaskTime.tasks = new SK_Map();
/**
 * 开启任务
 * @param {Object} callback
 * @param {Object} date
 * @param {Object} groupName
 * @param {Object} jobName
 */
SK_TaskTime.start = function(callback, date, groupName, jobName){
    SK_Tool.log('SK_TaskTime.start  GroupName:' + groupName + ' JobName:' + jobName)
    var task = new SK_Task(callback, date, groupName, jobName);
    var group = SK_TaskTime.tasks.get(groupName);
    if (!group) {
    	group = new SK_Map();
        SK_TaskTime.tasks.put(groupName, group);
        
    }
    var job = group.get(jobName);
    if (!job) {
        job = new SK_Map();
        var timeOut = setTimeout(task.run, date);
        group.put(jobName, timeOut);
    }
}
/**
 * 结束任务
 * @param {Object} groupName
 * @param {Object} jobName
 */
SK_TaskTime.stop = function(groupName, jobName){
    SK_Tool.log('SK_TaskTime.stop  GroupName:' + groupName + ' JobName:' + jobName)
    var group = SK_TaskTime.tasks.get(groupName);
    if (group) {
        var timeOut = group.get(jobName);
        if (timeOut) {
            clearTimeout(timeOut);
            group.remove(jobName);
            if (group.isEmpty()) {
                group.remove(groupName);
            }
        }
    }
}
/**
 * 更新任务
 * @param {Object} callback
 * @param {Object} date
 * @param {Object} groupName
 * @param {Object} jobName
 */
SK_TaskTime.update = function(callback, date, groupName, jobName){
    SK_Tool.log('SK_TaskTime.update  GroupName:' + groupName + ' JobName:' + jobName)
    SK_TaskTime.stop(groupName, jobName);
    SK_TaskTime.start(callback, date, groupName, jobName);
}

/**
 * 点类型
 * @param {Object} x
 * @param {Object} y
 * @param {Object} z
 */
function SK_Vector(x, y, z){
    this.x = x;
    this.y = y;
    this.z = z;
    //计算两点的距离
    this.distance = function(vec){
        var _x = Math.abs(this.x - vec.x);
        var _y = Math.abs(this.y - vec.y);
        var sum = Math.pow(_x, 2) + Math.pow(_y, 2);
        return Math.sqrt(sum);
    }
}

/**
 * 时间类型
 */
function SK_Date(date){
    this.date = date;
    
    this.addWeek = function(w){
        var time = this.date.getMilliseconds() + (w * (SK_Date.TIME_DAY * 7));
        this.date.setMilliseconds(time);
        return this.date;
    }
    
    this.addDay = function(d){
        var time = this.date.getMilliseconds() + (d * SK_Date.TIME_DAY);
        this.date.setMilliseconds(time);
        return this.date;
    }
    
    this.addHour = function(h){
        var time = this.date.getMilliseconds() + (h * SK_Date.TIME_HOUR);
        this.date.setMilliseconds(time);
        return this.date;
    }
    
    this.addMinute = function(m){
        var time = this.date.getMilliseconds() + (m * SK_Date.TIME_MINUTE);
        this.date.setMilliseconds(time);
        return this.date;
    }
    
    this.addSecond = function(s){
        var time = this.date.getMilliseconds() + (m * SK_Date.TIME_SECOND);
        this.date.setMilliseconds(time);
        return this.date;
    }
    
    this.addMillisSecond = function(ms){
        var time = this.date.getMilliseconds() + (ms * SK_Date.TIME_MILLISECOND);
        this.date.setMilliseconds(time);
        return this.date;
    }
}

SK_Date.TIME_MILLISECOND = 1;
SK_Date.TIME_SECOND = 1000 * SK_Date.TIME_MILLISECOND;
SK_Date.TIME_MINUTE = 60 * SK_Date.TIME_SECOND;
SK_Date.TIME_HOUR = 60 * SK_Date.TIME_MINUTE;
SK_Date.TIME_DAY = 24 * SK_Date.TIME_HOUR;
SK_Date.TIME_WEEK = 7 * SK_Date.TIME_DAY;
